package physicsModeler;

import java.util.Vector;
import java.io.File;
import physicsModeler.environment.DrawEnvironment;
import physicsModeler.objects.DrawObject;

public class EnvironmentFactory {
	
	String path;
	
	Vector envNames;
	
	public EnvironmentFactory()
	{
		init(".\\bin\\physicsModeler\\environment\\");
		
	}
	
	public void init(String path)
	{
		Class c = null;
		Object o = null;
		String className = "";
		
		try{
		envNames = new Vector();
		File file = new File(path);
		
		String fileNames[] = file.list();
		
		for (int i = 0; i < fileNames.length; i++) { // Go through all files
            if (fileNames[i].endsWith(".class") == true) { // Ignore if not a Java .class file
                 if (fileNames[i].compareTo("DrawEnvironment.class") != 0) { // Skip the DrawObject base class
                      // Try to load the class
                      try {
                           // Strip ".class" from the end
                           className = fileNames[i].substring(0, fileNames[i].length() - 6);

                           // Load the class and create an instance of it
                           c = Class.forName("physicsModeler.environment." + className);
                           o = c.newInstance();
                      } catch (Exception ex) {
                           ex.printStackTrace();
                      }

                      // Verify that it is a DrawObject.  If so, add its class name to the
                      // shape list.
                      if (o instanceof DrawEnvironment) {
                           envNames.addElement(className);
                      }
                 }
            }
       }
		
		}// end try block
		catch(Exception ex)
		{
			ex.printStackTrace();
			
		}
		
	} // end init
	
	public Vector getEnvNames() {
	    return (Vector)envNames.clone();
	  }
	
	public DrawEnvironment createDrawEnvironment(String objectName) throws Exception {
	    try {
	      Class c = Class.forName("physicsModeler.environment." + objectName);
	      DrawEnvironment d = (DrawEnvironment)c.newInstance();
	      return d;
	    }
	    catch (Exception ex) {
	      throw ex;
	    }
	  }
	

}
